﻿using System;
using System.Diagnostics;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;

namespace AyuBookmark.WinForm.Dialogs
{
    /// <summary>
    /// 关于对话框（显示应用程序信息）
    /// </summary>
    public partial class AboutDialog : Form
    {
        private PictureBox logoPictureBox;
        private Label lblAppName;
        private Label lblVersion;
        private Label lblCopyright;
        private Label lblDescription;
        private LinkLabel linkWebsite;
        private Button btnClose;
        private TableLayoutPanel tableLayoutPanel;

        public AboutDialog()
        {
            InitializeComponent();
            LoadApplicationInfo();
        }

        private void InitializeComponent()
        {
            // 窗体基础设置
            this.SuspendLayout();
            this.Text = "关于";
            this.StartPosition = FormStartPosition.CenterParent;
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.ClientSize = new Size(450, 300);
            this.Padding = new Padding(10);
            this.BackColor = Color.White;

            // 表格布局（用于自动调整控件位置）
            this.tableLayoutPanel = new TableLayoutPanel();
            this.tableLayoutPanel.Dock = DockStyle.Fill;
            this.tableLayoutPanel.ColumnCount = 2;
            this.tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 120));
            this.tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100));
            this.tableLayoutPanel.RowCount = 5;
            this.tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 120));
            this.tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 40));
            this.tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 30));
            this.tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 100));
            this.tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 50));

            // 应用程序图标
            this.logoPictureBox = new PictureBox();
            this.logoPictureBox.SizeMode = PictureBoxSizeMode.Zoom;
            this.logoPictureBox.Dock = DockStyle.Fill;
            this.logoPictureBox.BackColor = Color.Transparent;

            // 应用程序名称
            this.lblAppName = new Label();
            this.lblAppName.Font = new Font("Microsoft YaHei", 14f, FontStyle.Bold);
            this.lblAppName.AutoSize = true;
            this.lblAppName.Dock = DockStyle.Bottom;

            // 版本信息
            this.lblVersion = new Label();
            this.lblVersion.Font = new Font("Microsoft YaHei", 9f);
            this.lblVersion.AutoSize = true;
            this.lblVersion.Dock = DockStyle.Bottom;

            // 版权信息
            this.lblCopyright = new Label();
            this.lblCopyright.Font = new Font("Microsoft YaHei", 8f);
            this.lblCopyright.AutoSize = true;
            this.lblCopyright.Dock = DockStyle.Bottom;

            // 描述文本
            this.lblDescription = new Label();
            this.lblDescription.Font = new Font("Microsoft YaHei", 9f);
            this.lblDescription.Dock = DockStyle.Fill;
            this.lblDescription.TextAlign = ContentAlignment.MiddleLeft;

            // 网站链接
            this.linkWebsite = new LinkLabel();
            this.linkWebsite.Font = new Font("Microsoft YaHei", 9f);
            this.linkWebsite.AutoSize = true;
            this.linkWebsite.LinkBehavior = LinkBehavior.HoverUnderline;
            this.linkWebsite.LinkClicked += OnWebsiteLinkClicked;

            // 关闭按钮
            this.btnClose = new Button();
            this.btnClose.Text = "关闭";
            this.btnClose.DialogResult = DialogResult.OK;
            this.btnClose.Size = new Size(80, 30);
            this.btnClose.Font = new Font("Microsoft YaHei", 9f);
            this.btnClose.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;

            // 添加控件到表格布局
            this.tableLayoutPanel.Controls.Add(this.logoPictureBox, 0, 0);
            this.tableLayoutPanel.SetRowSpan(this.logoPictureBox, 3);
            this.tableLayoutPanel.Controls.Add(this.lblAppName, 1, 0);
            this.tableLayoutPanel.Controls.Add(this.lblVersion, 1, 1);
            this.tableLayoutPanel.Controls.Add(this.lblCopyright, 1, 2);
            this.tableLayoutPanel.Controls.Add(this.lblDescription, 0, 3);
            this.tableLayoutPanel.SetColumnSpan(this.lblDescription, 2);
            this.tableLayoutPanel.Controls.Add(this.linkWebsite, 0, 4);
            this.tableLayoutPanel.SetColumnSpan(this.linkWebsite, 2);
            this.tableLayoutPanel.Controls.Add(this.btnClose, 1, 4);
            this.tableLayoutPanel.SetColumnSpan(this.btnClose, 2);

            // 添加表格布局到窗体
            this.Controls.Add(this.tableLayoutPanel);

            this.ResumeLayout(false);
        }

        private void LoadApplicationInfo()
        {
            // 获取程序集信息
            var assembly = Assembly.GetExecutingAssembly();
            var versionInfo = FileVersionInfo.GetVersionInfo(assembly.Location);

            // 设置控件值
            this.lblAppName.Text = versionInfo.ProductName ?? "AyuBookmark";
            this.lblVersion.Text = $"版本: {assembly.GetName().Version}";
            this.lblCopyright.Text = versionInfo.LegalCopyright ?? "Copyright © 2023";
            this.lblDescription.Text = versionInfo.Comments ?? "一款简洁高效的书签管理工具";
            this.linkWebsite.Text = versionInfo.CompanyName ?? "https://github.com/yourrepo";

            // 加载图标（使用默认图标）
            this.logoPictureBox.Image = LoadDefaultLogo();
        }

        private Image LoadDefaultLogo()
        {
            // 创建默认图标（实际项目应使用资源文件）
            var bmp = new Bitmap(100, 100);
            using (var g = Graphics.FromImage(bmp))
            {
                g.Clear(Color.Transparent);
                g.FillEllipse(Brushes.DodgerBlue, 0, 0, 99, 99);
                g.DrawString("AB", new Font("Arial", 24, FontStyle.Bold), Brushes.White, 25, 30);
            }
            return bmp;
        }

        private void OnWebsiteLinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                Process.Start(new ProcessStartInfo
                {
                    FileName = this.linkWebsite.Text.StartsWith("http") ?
                        this.linkWebsite.Text :
                        "https://" + this.linkWebsite.Text,
                    UseShellExecute = true
                });
            }
            catch (Exception ex)
            {
                MessageBox.Show($"无法打开链接: {ex.Message}", "错误",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public static void ShowAbout(IWin32Window owner)
        {
            using (var dialog = new AboutDialog())
            {
                dialog.ShowDialog(owner);
            }
        }
    }
}